// 前置首位
import { router, addRoutes } from '@/router/index.js'
import { getToken } from '@/composables/auth.js'
import { Notify, loadingShow, loadingHidden } from '@/composables/util.js'
import store from '@/store'

// import { useRoute } from 'vue-router'
// 全局前置守卫
let hasGetInfo = false
router.beforeEach(async (to, from, next) => {
  console.log(11111, '前置');
  // 显示loading
  loadingShow()
  const token = getToken()
  // 没登陆，强制跳转
  if (!token && to.path != "/login") {
    Notify('请先登录', 'error')
    return next({ path: "/login" })
  }
  // 防止重复登陆
  if (token && to.path == "/login") {
    Notify('请勿重复登录', 'error')
    return next({ path: from.path ? from.path : "/" })
  }
  let hasNewRoutes = false
  if (token && !hasGetInfo) {
    let { menus } = await store.dispatch('getInfo')
    hasGetInfo = true
    hasNewRoutes = addRoutes(menus)
  }
  // 设置动态标题

  let title = (to.meta.title ? to.meta.title : '') + '~小王'
  document.title = title
  // 返回 false 以取消导航
  // console.log('hasNewRoutes', hasNewRoutes)
  // console.log('fullPath', to.fullPath)
  hasNewRoutes ? next(to.fullPath) : next()
  // console.log('from',from);
  // console.log();

  // //  if(hasNewRoutes&& to.fullPath == ){

  // //  }
  //   console.log('useRoute',useRoute());
  // next()
})
// 全局后置
router.afterEach((to, from) => {
  // 关闭loading
  loadingHidden()
})

